package com.cx.controller;

import com.cx.pojo.GoodCollection;
import com.cx.pojo.Users;
import com.cx.service.CollectService;
import com.cx.util.RequestResult;
import com.cx.vo.GoodCollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: Zhou wei
 * @create: 2020/5/19 20:35
 */
@Controller
@RequestMapping("collect")
public class CollectController {

    @Autowired
    CollectService collectService;

    /**
     * 跳转收藏夹页面并显示数据
     * @param
     * @return
     */
    @RequestMapping("favorite")
    public ModelAndView selectFromGoods(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            modelAndView.setViewName("redirect:/index/login");
            return modelAndView;
        }
        List<GoodCollectVO> goodCollectVOList = collectService.selectFromGoods(user.getId());
        modelAndView.addObject("goodCollectVOList",goodCollectVOList);
        modelAndView.setViewName("favorite");
        return modelAndView;
    }



    /**
     * 添加收藏
     * @param userId
     * @param goodId
     * @return
     */
    @RequestMapping("addcollect")
    @ResponseBody
    public RequestResult addCollect(Integer userId, Integer goodId){
        int affectedRows = collectService.addCollect(userId,goodId);
        if (affectedRows > 0){
            return new RequestResult(1,"收藏成功",null);
        } else {
            return new RequestResult(2,"收藏失败",null);
        }

    }

    /**
     * 取消收藏
     * @param userId
     * @param goodId
     * @return
     */
    @RequestMapping("deletecollect")
    @ResponseBody
    public RequestResult deleteCollect(Integer userId,Integer goodId){
        int affectedRows = collectService.deleteCollect(userId,goodId);
        if (affectedRows > 0){
            return new RequestResult(1,"取消成功",null);
        } else {
            return new RequestResult(1,"取消失败",null);
        }
    }

    /**
     * 判断该商品是否已被收藏
     * @param userId
     * @param goodId
     * @return
     */
    @RequestMapping("iscollect")
    @ResponseBody
    public RequestResult selectByUserIdAndGoodId(Integer userId,Integer goodId){
        List<GoodCollection> goodCollectionList = collectService.selectByUserIdAndGoodId(userId,goodId);
        if (goodCollectionList != null){
            return new RequestResult(1,"该商品已被收藏",null);
        } else {
            return new RequestResult(1,"该商品未被收藏",null);
        }
    }


    @RequestMapping("selectbygoodname")
    @ResponseBody
    public RequestResult selectByGoodName(Integer userId,String goodName){
        List<GoodCollectVO> goodCollectVOList = collectService.selectByGoodName(userId,goodName);
        return new RequestResult(1,"查询成功",goodCollectVOList);
    }


//    public static void main(String[] args) {
//        A a = new B();
//        a = new B();
//    }




}

//class A{
////    static{
////        System.out.println("1");
////    }
////    public A(){
////        System.out.println("2");
////    }
////}
////
////class B extends A{
////    static{
////        System.out.println("a");
////    }
////    public B(){
////        System.out.println("b");
////    }
////}